package com.mtk.launcher.view;

import android.content.Context;
import android.view.View;
import android.widget.TextView;


import com.mtk.launcher.R;
import com.mtk.launcher.bean.TimeCardEntity;
import com.mtk.launcher.util.TimeChangeUtil;
import com.mtk.launcher.util.TimerDealUtil;

import java.util.Calendar;

/***
 * 卡片样式得布局
 */
public class TimeCardGenerate extends LayoutViewBase {

    View view;

    public TimeCardGenerate(Context context, int x, int y, int width, int height) {
        super(x, y, width, height);
        view = View.inflate(context, R.layout.view_time_card_new, null);
        initView();
        TimerDealUtil.getInstance().addGeneratorToList(TimeCardGenerate.this);
    }

    TextView tv_week, tv_date;
    TimeCardView tv_hour_1, tv_hour_2, tv_min_1, tv_min_2, tv_sec_1, tv_sec_2;

    private void initView() {
        tv_week = view.findViewById(R.id.tv_week);
        tv_date = view.findViewById(R.id.tv_date);

        tv_hour_1 = view.findViewById(R.id.tv_hour_1);
        tv_hour_2 = view.findViewById(R.id.tv_hour_2);
        tv_min_1 = view.findViewById(R.id.tv_min_1);
        tv_min_2 = view.findViewById(R.id.tv_min_2);

        tv_sec_1 = view.findViewById(R.id.tv_sec_1);
        tv_sec_2 = view.findViewById(R.id.tv_sec_2);
    }

    @Override
    public void timeChangeToUpdateView() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1; // 月份从0开始，需要加1
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        tv_week.setText(weekDayShow(week));
        tv_date.setText(day + " / " + month + " / " + year);
        TimeCardEntity timeCardEntity = TimeChangeUtil.getCurrentTime();
        if (timeCardEntity == null) {
            return;
        }
        tv_hour_1.showCurrentNum(timeCardEntity.getTimeHour1());
        tv_hour_2.showCurrentNum(timeCardEntity.getTimeHour2());
        tv_min_1.showCurrentNum(timeCardEntity.getTimeMin1());
        tv_min_2.showCurrentNum(timeCardEntity.getTimeMin2());
        tv_sec_1.showCurrentNum(timeCardEntity.getTimeSec1());
        tv_sec_2.showCurrentNum(timeCardEntity.getTimeSec2());
    }

    private String weekDayShow(int week) {
        String backDay = "";
        switch (week) {
            case 1:
                backDay = "Mon";
                break;
            case 2:
                backDay = "Tue";
                break;
            case 3:
                backDay = "Wed";
                break;
            case 4:
                backDay = "Thur";
                break;
            case 5:
                backDay = "Fri";
                break;
            case 6:
                backDay = "Sat";
                break;
            case 0:
            case 7:
                backDay = "Sun";
                break;
            default:
                backDay = "Sun";
                break;
        }
        return backDay;
    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void clearMemory() {
        TimerDealUtil.getInstance().removeGeneratorToList(TimeCardGenerate.this);
    }

}
